import React, { useRef, useState } from 'react';
import { AxTextForCard as AxText } from '@/components/AxBase';
import Search from './components/search';
import Create from './components/create';
import ManagerCreate from './components/managerCreate';
import { router } from 'umi';
// import './style.css';
import { AxStandardCardButtonType, AxStandardCardFilterType } from '@/components/AxList/AxStandardLayout/data.d';
import { AxStandardCard } from '@/components/AxList/AxStandardCard';
import { axurl } from '@/shared/axurl';
import Update from './components/update';
import { axhttp } from '@/shared/axhttp';
import { message } from '_antd@3.26.20@antd';

const url = axurl.lab.list.data;
const { postFormX } = axhttp;
const Index: React.FC<any> = props => {
    const [createVisible, setCreateVisible] = useState(false);
    const [updateVisible, setUpdateVisible] = useState(false);
    const [operateValues, setOperateValues] = useState({});
    const [managerCreateVisible, setManagerCreateVisible] = useState(false);
    const ref = useRef<any>({});

    /** 配置操作按钮 */
    const button: AxStandardCardButtonType[] = [
        // {
        //     text: '申请负责人',
        //     icon: 'plus',
        //     onClick: () => setManagerCreateVisible(true),
        // },
        // {
        //     text: '申请使用采购主体',
        //     icon: 'plus',
        //     onClick: () => setCreateVisible(true),
        // }
    ];

    const filter: AxStandardCardFilterType[] = [
        // {
        //     icon: 'plus-circle',
        //     text: '负责人',
        //     field: 'isManager',
        //     value: true,
        // }, {
        //     icon: 'minus-circle',
        //     text: '使用人',
        //     field: 'isManager',
        //     value: false,
        // }
    ]

    const createProps = {
        visible: createVisible,
        handleVisible: setCreateVisible,
    };

    const managerCreateProps = {
        visible: managerCreateVisible,
        handleVisible: setManagerCreateVisible,
    };

    const handleData = (res: any) => {
        return {
            ...res,
            list: res.list.map((i: any) => ({
                id: i.id,
                img: <img src='img/store.png' width={100} />,
                desc: (
                    <>
                        <AxText>
                            {i.userName}&nbsp;
                            <span className={`ax-manager ${i.isManage === false && 'ax-user'}`}>{i.isManage && '负责人' || '使用人'}</span>&nbsp;
                            <a onClick={(e) => handleUpdateValues(e, true, i)}>{i.isManage && '修改'}</a>
                        </AxText>
                        <AxText>{i.name}</AxText>
                        <AxText>{i.address}</AxText>
                        <AxText>所属部门：{i.deptName}</AxText>
                    </>
                ),
                click: () => router.push({ pathname: '/buyer/lab/inventory/' }),
            }))
        }
    }


    /** 获取要修改的行信息，并展示修改弹框 */
    const handleUpdateValues = (e: any, flag: any, record: any) => {
        if (e) {
            e.preventDefault();
            e.stopPropagation();
        }
        if (record) {
            setUpdateVisible(flag);
            setOperateValues(record);
        }
    };

    const handleUpdate = (fv: any) => {
        return postFormX(url.update, null, fv).then(() => {
            message.success('修改成功');
            setUpdateVisible(false);
            ref.current.handleRefresh();
        });
    }

    const updateProps = {
        visible: updateVisible,
        values: operateValues,
        handleVisible: setUpdateVisible,
        handleUpdate: handleUpdate,
    };

    return (
        <>
            <AxStandardCard
                ref={ref}
                url={url}
                button={button}
                filter={filter}
                AdvancedSearch={Search}
                requestMethod='getDataX'
                handleData={handleData}
                simpleSearchField='name'
                simpleSearchPlaceholder='输入采购主体名称/编码/地址搜索'
            />
            {createVisible && <Create {...createProps} />}
            {updateVisible && <Update {...updateProps} />}
            {managerCreateVisible && <ManagerCreate {...managerCreateProps} />}
        </>
    );
};

export default Index;
